package com.saibo.spider.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * String以及Object操作工具
 * 
 * @author zhangzhening
 * @version 0.0.1
 */
public class StringUtil {
	/**
	 * if "" or null ,return true else return false
	 * 
	 * @param obj
	 * @return
	 * @author zhangzhening
	 */
	public static boolean isNullOrEmpty(Object obj) {
		if ("".equals(obj) || null == obj) {
			return true;
		}
		return false;
	}

	/**
	 * param -> String a = "a" or String[] a = {"a","b","c","d"} <br/>
	 * println
	 * 
	 * @param str
	 */
	public static void print(String... str) {
		for (String object : str) {
			System.out.println(object.toString());
		}
	}

	/**
	 * 将Map&lt;String,Object&gt;对象转化为Json字符串 <br/>
	 * 
	 * @param map
	 * @return
	 */
	public static String mapToJson(Map<String, Object> map) {
		return JSON.toJSONString(map);
	}

	public static String objectToJson(Object obj) {
		return JSON.toJSONString(obj);
	}

	/**
	 * 根据Unicode编码完美的判断中文汉字和符号
	 * 
	 * @param c
	 * @return
	 */
	public static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}

	/**
	 * 根据Json返回一个Map类型的对象
	 * 
	 * @param json
	 * @return
	 */
	public static Map<String, Object> jsonToMap(String json) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			JSONObject jsonMap = JSONObject.parseObject(json);
			for (java.util.Map.Entry<String, Object> entry : jsonMap.entrySet()) {
				returnMap.put(entry.getKey(), entry.getValue());
			}
			return returnMap;
		} catch (Exception e) {
		}
		return returnMap;
	}

	public static class HtmlFilter {
		public String regex;
		public String replacement;

		public HtmlFilter(String regex, String replacement) {
			this.regex = regex;
			this.replacement = replacement;
		}

		public HtmlFilter(String regex) {
			this.regex = regex;
			this.replacement = "";
		}

	}

	/**
	 * Html清理策略
	 * 
	 * @param resp
	 * @return
	 */
	public static String trimHtml(String resp) {
		if (resp == null || resp.isEmpty()) {
			return "";
		}
		String xml = resp;
		HtmlFilter[] filter = getReplaceList();
		if (filter != null && filter.length > 0) {
			for (HtmlFilter filte : filter) {
				xml = xml.replaceAll(filte.regex, filte.replacement);
			}
		}
		return xml;
	}

	/**
	 * html清理策略. 大多数时候重写这个方法就足够适配各种页面了.
	 * 
	 * @see #trimHtml(String)
	 * @return
	 */
	protected static HtmlFilter[] getReplaceList() {
		return new HtmlFilter[] { new HtmlFilter("\\s{2,}", " "), new HtmlFilter("<style.*?</style>", ""),
				new HtmlFilter("<script.*?</script>", ""), new HtmlFilter("</?(meta|link).*?>", "") };
	}

	/**
	 * 获取Element中的文本信息. e.g. <span><em>sybn<em>123</span> ==> sybn123
	 * 
	 * @param element
	 * @return
	 */
	public static String element2text(Element element) {
		if (element == null) {
			return "";
		}
		String asXML = element.html();
		if (asXML == null) {
			return "";
		}
		asXML = asXML.replace("\r", "");
		asXML = asXML.replace("\n", "");
		asXML = asXML.replace("&nbsp;", " ");
		asXML = asXML.replace("&ensp;", " ");
		asXML = asXML.replace("&emsp;", " ");
		asXML = asXML.replace("&#160;", " ");
		asXML = asXML.replaceAll("\\s{1,}", " ");
		asXML = asXML.replaceAll("<script[^>]*>[\\d\\D]*?</script>", "");
		asXML = asXML.replaceAll("<style[^>]*>[\\d\\D]*?</style>", "");
		return asXML.replaceAll("<.*?>", "");
	}

	public static String clean(String html) {
		html = html.replaceAll("\"", "\"");
		html = html.replaceAll("\n", "");
		html = html.replaceAll("\t", "");
		html = html.replaceAll("\\/", "/");
		html = html.replaceAll("", "");
		html = html.replaceAll("", "");
		html = html.replaceAll("", "");
		html = html.replaceAll("", "");
		html = html.replaceAll("", "");
		html = html.replaceAll("", "");
		html = html.replaceAll("", "");
		return html;
	}

	public static String cleanEncode(String str){
		str = str.replaceAll("&amp;", "&");
		str = str.replaceAll("&quot;", "'");
		str = str.replaceAll("&nbsp;", " ");
		str = str.replaceAll("&lt;", "<");
		str = str.replaceAll("&gt;", ">");
		str = str.replaceAll("&apos;", "'");
		str = str.replaceAll("&yen;", "¥");
		str = str.replaceAll("&euro;", "€");
		str = str.replaceAll("&copy;", "©");
		str = str.replaceAll("&reg;", "®");
		return str;
	}
}
